﻿using CommerceServer.Core;
using CommerceServer.Core.Catalog;
using CommerceServer.Core.Inventory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ImportInventory
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length < 2 || args[0] == "/?")
            {
                PrintUsage();
                return;
            }

            var catalogSiteAgent = new CommerceServer.Core.Catalog.CatalogSiteAgent();

            Console.WriteLine(string.Format("Importing inventory for site {0} using file {1}", args[0], args[1]));

            catalogSiteAgent.SiteName = args[0];
            catalogSiteAgent.IgnoreInventorySystem = false;

            if (args.Length >= 3)
            {
                Console.WriteLine(string.Format("AzMan file provided {0}", args[2]));
                catalogSiteAgent.AuthorizationPolicyPath = args[2];
                catalogSiteAgent.AuthorizationMode = AuthorizationMode.ThreadContext; 
            }

            var catalogContext = CommerceServer.Core.Catalog.CatalogContext.Create(catalogSiteAgent);

            ImportInventoryCatalog(catalogContext.InventoryContext, args[1]);

            Console.WriteLine("Finished importing inventory catalog...");
        }

        public static void ImportInventoryCatalog(InventoryContext inventoryContext, string inventoryFile)
        {
            // 3 second refresh interval.
            const int RefreshInterval = 3000;

            // Create InventoryImportOptions and set the options 
            // for the import operation.
            InventoryImportOptions importOptions = new InventoryImportOptions();

            // Import and replace all catalogs.
            importOptions.Mode = ImportMode.Full;
            importOptions.TransactionMode = TransactionMode.NonTransactional;
            importOptions.Operation = ImportOperation.Import;
            importOptions.ErrorThreshold = 100;
            importOptions.ImportSchemaChanges = true;
            importOptions.ExportReady = true;

            // The XML file is in the temp directory on this computer.
            string fileName = inventoryFile;

            // Monitor the progress of the import operation.
            ImportProgress importProgress = inventoryContext.ImportXml(importOptions, fileName);
            while (importProgress.Status == CatalogOperationsStatus.InProgress)
            {
                System.Threading.Thread.Sleep(RefreshInterval);
                importProgress.Refresh();
            }

            // If the import operation failed, write the errors to the console.
            if (importProgress.Status == CatalogOperationsStatus.Failed)
            {
                foreach (CatalogError error in importProgress.Errors)
                {
                    Console.WriteLine(string.Format("Line number: {0} Error message: {1}",
                        error.LineNumber, error.Message));
                }
            }
        }


        public static void PrintUsage()
        {
            Console.WriteLine("Used to import a inventory catalog and update the associated AzMan file using the local API");

            Console.WriteLine();

            Console.WriteLine(@"ImportInventory.exe SiteName c:\inventory.xml [C:\inetpub\CSServices\SolutionStorefrontSite_CatalogWebService\CatalogAuthorizationStore.xml]");
        }
    }
}
